﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace App
{
    public partial class MessageList : System.Web.UI.Page
    {
        //ID tematu
        int ThreadID;

        int TeamID;

        AppLib.Team team;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Common.GetLoggedUser(this.Page) == null)
            {
                Response.Redirect("Login.aspx");
                return;
            }

            ThreadID = int.Parse(Server.HtmlEncode(Request.Cookies["MessageList"].Value));
            
            TeamID = int.Parse(Server.HtmlEncode(Request.Cookies["ThreadList"].Value));

            team = MidApp.ManageTeam.GetTeamByID(TeamID);
            ImageAvatar.DefaultImage = team.ImageURL;
            ImageAvatar.Email = team.TeamID.ToString();
            LinkButtonTeamInfo.Text = team.TeamName;

            TopicName.Text = "(" + MidApp.ManageTopic.GetThread(ThreadID).Title + ")";
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            RepeaterMessagesList.DataSource = MidApp.ManageMessage.GetMessagesAssignedToTopic(ThreadID);
            RepeaterMessagesList.DataBind();
        }
        protected void ButtonNewMesage_Click(object sender, EventArgs e)
        {
            AppLib.Message aux = new AppLib.Message();
            aux.Inserted = DateTime.Now;
            aux.Report = TextBoxNewMessage.Text;
            aux.UserID = Common.GetLoggedUser(this.Page).UserID;

            MidApp.ManageMessage.CreateNewMessage(aux, ThreadID);
            
            TextBoxNewMessage.Text = "";
            LabelInfo.Visible = true;
            collapsiblePanelExtender.Collapsed = true;
        }
        //vraci jmeno tematu
        protected string GetThread()
        {
            return MidApp.ManageTopic.GetThread(ThreadID).Title;
        }
        //vraci nick uzivatele
        protected string GetNick(object sender)
        {
            var aux = (AppLib.Message)sender;

            return aux.User.Nick;
        }
        protected bool IsMessageOwner(object sender)
        {
            var aux = (AppLib.Message)sender;

            if (aux.UserID == Common.GetLoggedUser(this.Page).UserID)
            {
                return true;
            }
            else
            {
                return false;
            }

        }
        //obsluha tlacitka pro zobrazeni detailu hrace
        protected void LinkButtonUserInfo_Click(object sender, EventArgs e)
        {
            Response.Cookies["UserDetail"].Value = ((LinkButton)sender).CommandArgument;
            Response.Redirect("UserDetail.aspx");
        }

        //obsluha pro presmerovani na detail tymu
        protected void LinkButtonTeamInfo_Click(object sender, EventArgs e)
        {
            Response.Cookies["TeamDetail"].Value = TeamID.ToString();
            Response.Redirect("TeamDetails.aspx");
        }
        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Response.Redirect("ThreadList.aspx");
        }
        protected void LinkButtonEditMessage_Click(object sender, EventArgs e)
        {
            Response.Cookies["MessageID"].Value = ((LinkButton)sender).CommandArgument;
            Response.Redirect("EditMessage.aspx");
        }

        protected string GetMemberEmail(object sender)
        {
            var aux = sender as AppLib.Message;

            try
            {
                return MidApp.ManageUser.GetUserByID((int)aux.UserID).PrimaryEmail;
            }
            catch (InvalidCastException)
            {
                return MidApp.ManageUser.GetUserByID((int)aux.UserID).PrimaryEmail;
            }

        }
        protected string GetMemberAvatar(object sender)
        {
            var aux = (AppLib.Message)sender;

            try
            {
                
                return MidApp.ManageUser.GetUserImage((int)aux.UserID);
            }
            catch (InvalidCastException)
            {
                return MidApp.ManageUser.GetUserImage((int)aux.UserID);
            }

        }
        protected string GetTeamID()
        {
            //return TeamID.ToString();
            return "";
        }
        protected string GetTeamAvatar()
        {
            //var aux = MidApp.ManageTeam.GetTeamByID(TeamID).ImageURL;

            //return aux;
            return "";

        }
    }
}